<?php
/**
 * 员工管理
 *
 * @author 孙晓晔
 * @version $Id: Emp.class.php 1043 2010-04-01 08:04:38Z sunxy $s
 */

require_once(dirname(__FILE__) . '/EmpUtil.class.php');

class Emp extends EmpUtil {
	public $tbl = Constant::tbl_emp;

	var $info_dir = 'emp';

	public $book = array(
		'-' => '请选择',
		'1' => '是',
		'2' => '否',		
	);
	
	public $state = array(
		'-' => '请选择',
		'1' => '侯选',
		// '2' => '笔试',
		// '3' => '面试',
		
		'4' => '试用',
		'5' => '正式',
		'6' => '外聘',
		'7' => '辞职',
		'8' => '离职',
	
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'name' => '姓名',
		'sex' => '性别',
		'dept' => '部门',
		// 'job' => '职位',
		'mobile' => '手机',
		'exten' => '分机',
		'email' => '电子邮件',
		'im' => '即时消息',
	
		'orderby' => '排序',
		'1' => '创建时间',
		'2' => '更新时间',
	);

	public $groupby = array(
		'-' => '请选择',
		'dept' => '部门',
		'sex' => '性别',
		'nationality' => '民族',
		'education' => '学历',
		'marriage' => '婚姻',
	
		'province' => '省',
		'city' => '市',
		'district' => '县',

		'2' => '日期',
		'3' => '月份',
	);

	public static $a1 = "im a1!<br/>";

	function __construct() {
		$request['update'] = '$Date: 2009/03/06 11:35:48 $';
		$request['revision'] = '$Revision: 1.2 $';

		parent::__construct($request);
	}

	/**
	 * 预处理
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function pretreat($controller) {
		parent::pretreat($controller);
		
		$controller->assign_by_ref('state_option', $this->state);
		$controller->assign_by_ref('book_option', $this->book);
	}
	
	function mobile($controller, $request) {
		$this->pretreat($controller);
	
		global $CategoryTree;
	
		$data = $CategoryTree['员工管理'];
	
		$ret = array();
		foreach ($data as $k => $v) {
			if ($this->tree_my($v)) {
				$ret[$k] = $v;
			}
		}
	
		$controller->assign('menu', $ret);
	
		$controller->display($request, 'mobile');
		return true;
	}

	/**
	 * 首页
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function tab($controller, $request) {		
		$data = array(
			'员工信息' => array('cc'=>'emp.php?do=Emp.view_id', 'js'=>''),
				
			'基本信息' => array('cc'=>'emp.php?do=Emp.find', 'js'=>'1'),
		    '个人资料' => array('cc'=>'emp.php?do=Info.find', 'js'=>'1'),
				
			'入职信息' => array('cc'=>'emp.php?do=EmpJob.find', 'js'=>'1'),
		
			'合同信息' => array('cc'=>'emp.php?do=Contract.find', 'js'=>'1'),
			'工资信息' => array('cc'=>'emp.php?do=Salary.find', 'js'=>'1'),
				
			'家庭关系' => array('cc'=>'emp.php?do=Family.find', 'js'=>'1'),
			'学习经历' => array('cc'=>'emp.php?do=Education.find', 'js'=>'1'),
			'工作经历' => array('cc'=>'emp.php?do=Work.find', 'js'=>'1'),
				
			'奖励情况' => array('cc'=>'emp.php?do=Award.find', 'js'=>'1'),
			'惩罚情况' => array('cc'=>'emp.php?do=Punish.find', 'js'=>'1'),
		
			'每月工资' => array('cc'=>'finance.php?do=Salary.find', 'js'=>'1'),			
			'银行帐号' => array('cc'=>'emp.php?do=EmpBank.find', 'js'=>'1'),
		
			// '网络信息' => array('cc'=>'emp.php?do=Mac.find', 'js'=>''),	
			'分机信息' => array('cc'=>'emp.php?do=Exten.find', 'js'=>'1'),
			// '邮箱信息' => array('cc'=>'emp.php?do=Vpopmail.find', 'js'=>''),
			
			'部门信息' => array('cc'=>'info.php?do=Dept.find', 'js'=>'1'),
			'招聘职位' => array('cc'=>'info.php?do=Job.find', 'js'=>''),
		
			'统计分析' => array('cc'=>'emp.php?do=Emp.tj_tab', 'js'=>''),
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tab');
		return true;
	}
	
	function tj_tab($controller, $request) {		
		$data = array(
			'员工信息' => array('cc'=>'emp.php?do=Info.group', 'js'=>''),				
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tj_tab');
		return true;
	}
	
	function view_id($controller, $request) {
		$this->pretreat($controller);
	
		$controller->display($request, 'emp/emp/id');
		return true;
	}
	
	function view_idPost($controller, $request) {
	
		$id = $this->_input($request['emp_s']);
	
		$row = $this->_view($id);
		
		if (!$this->_view_id_my($row)) {
			$controller->assign('msg', "对不起，操作受限，请联系管理员。");
			
			$controller->display($request, 'info');
			return true;
		}
		
		$controller->assign_by_ref('result', $row);
		
		$ret = $this->_view_tab();
		
		$controller->assign_by_ref('tabs', $ret);
	
		$controller->display($request, 'emp/emp/info/view');
		return true;
	}
	
	function _view_id_my($data) {
		
		if ($this->_is_admin()) {
			return true;
		}
		
		$emp = $_SESSION['id'];
		
		$p = array('1'=>'12', '2'=>'13', '3'=>'14');
		
		$a = $this->_audit_office($p);
		if (count($a) > 0) {
			// $office = implode(",", $a);
			
			if (in_array($data['office'], $a)) {
				return true;
			}
			
			if ($data['emp'] == $emp || $data['oper'] == 'emp') {
				return true;
			}
		}
		
		return false;
	}

	function _view_tab() {
		$data = array(
			'个人资料' => array('cc'=>'emp.php?do=Info.view_emp&id=', 'js'=>'1'),
	
			'入职信息' => array('cc'=>'emp.php?do=EmpJob.view_emp&id=', 'js'=>'1'),
			'合同信息' => array('cc'=>'emp.php?do=Contract.view_emp&id=', 'js'=>'1'),
	
			'家庭关系' => array('cc'=>'emp.php?do=Family.view_emp&id=', 'js'=>'1'),
	
			'学习经历' => array('cc'=>'emp.php?do=Education.view_emp&id=', 'js'=>'1'),
			'工作经历' => array('cc'=>'emp.php?do=Work.view_emp&id=', 'js'=>'1'),
				
			'附件信息' => array('cc'=>'emp.php?do=Emp.view_file&id=', 'js'=>'1'),
	
			'银行帐号' => array('cc'=>'emp.php?do=EmpBank.view_emp&id=', 'js'=>'1'),
			'工资信息' => array('cc'=>'emp.php?do=Salary.view_emp2&id=', 'js'=>'1'),
			'每月工资' => array('cc'=>'finance.php?do=Salary.view_emp&id=', 'js'=>'1'),
				
			'请假信息' => array('cc'=>'kq.php?do=Vacation.view_emp2&id=', 'js'=>'1'),
			'报销信息' => array('cc'=>'finance.php?do=Expense.view_emp&id=', 'js'=>'1'),
			'付款信息' => array('cc'=>'finance.php?do=Payment.view_emp&id=', 'js'=>'1'),
	
			'用车需求' => array('cc'=>'car.php?do=Requirement.view_emp&id=', 'js'=>'1'),
			'货运需求' => array('cc'=>'express.php?do=Requirement.view_emp&id=', 'js'=>'1'),
			'任务信息' => array('cc'=>'mold.php?do=Plan.view_emp&id=', 'js'=>'1'),
				
			'所属角色' => array('cc'=>'emp.php?do=EmpRole.view_emp&id=', 'js'=>'1'),
			'所属权限' => array('cc'=>'emp.php?do=EmpPriv.view_emp&id=', 'js'=>'1'),
			'管理业务' => array('cc'=>'emp.php?do=EmpService.view_emp&id=', 'js'=>'1'),
			'便笺纸' => array('cc'=>'info.php?do=Note.view_emp&id=', 'js'=>'1'),
			'操作日志' => array('cc'=>'log.php?do=LogAction.view_emp&id=', 'js'=>'1'),
			
			'客户信息' => array('cc'=>'contact.php?do=Customer.view_emp&id=', 'js'=>'1'),
			'客户报价' => array('cc'=>'customer.php?do=Quote.view_emp&id=', 'js'=>'1'),
			'产品信息' => array('cc'=>'mold.php?do=Mold.view_emp&id=', 'js'=>'1'),
			
			'供应商' => array('cc'=>'contact.php?do=Supplier.view_emp&id=', 'js'=>'1'),
	
			'固定资产' => array('cc'=>'finance.php?do=Asset.view_emp&id=', 'js'=>'1'),
			'网络信息' => array('cc'=>'emp.php?do=Mac.view_emp&id=', 'js'=>'1'),
			'分机信息' => array('cc'=>'emp.php?do=Exten.view_emp&id=', 'js'=>'1'),
		);
		
		return $this->_tab2($data);
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'emp/emp/find');
		return true;
	}

	function _where($request) {
		$name_s = $request['name_s'];
		$name2_s = $request['name2_s'];

		$exten_s = $request['exten_s'];
		$email_s = $request['email_s'];

		$book_s = $request['book_s'];
		$state_s = $request['state_s'];
		
		$sex_s = $request['sex_s'];
		
		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}
		
		if (!empty($name2_s)) {
			$s = "%" . $name2_s . "%";
			$where .= sprintf(" AND (mobile LIKE '%s' OR email LIKE '%s' OR job LIKE '%s' OR content LIKE '%s')", $s, $s, $s, $s);
		}

		if (!empty($name_s)) {
			$where .= " AND name like '" . $name_s . "%'";
		}

		if (!empty($book_s) && $book_s <> '-') {
			$where .= " AND book = '" . $book_s . "'";
		}
		
		if (!empty($sex_s) && $sex_s <> '-') {
			$where .= " AND sex = '" . $sex_s . "'";
		}
		
		$where .= $this->_where_x("state", $request['state_s']);
		
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);
				
				$row['work_'] = $this->_num_work($row['id']);
				$row['bank_'] = $this->_num_bank($row['id']);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'emp/emp/list');
		return true;
	}

	function _num_work($id) {
		require_once(dirname(__FILE__) . '/Work.class.php');
		$aa = new Work();
		
		return $aa->num($id);
	}
	
	function _num_bank($id) {
		require_once(dirname(__FILE__) . '/../finance/Bank.class.php');
		$aa = new Bank();
		
		return $aa->num('1', $id);
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function xls($controller, $request) {
		$this->pretreat($controller);
		
		$this->findPara($controller, $request);

		$request['tpl'] = "xls";

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);
		
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);

			$row = $this->_orderby_x($controller, $row, $this->orderby_s);

			$data[] = $row;
		}
		
		$controller->assign_by_ref('result', $data);
		
		$filename = sprintf("emp_%s.xls", date("YmdHis"));
		header('Content-Disposition:application/vnd.ms-excel; filename=' . $filename);

		if ($request['xls'] == '1') {
			$controller->display($request, 'emp/emp/list_1');
		} else if ($request['xls'] == '2') {
			$controller->display($request, 'emp/emp/list_2');
		} else if ($request['xls'] == '3') {
			$controller->display($request, 'emp/emp/list_3');
		} else if ($request['xls'] == '4') {
			$controller->display($request, 'emp/emp/list_4');
		} else if ($request['xls'] == '5') {
			$controller->display($request, 'emp/emp/list_5');
		} elseif ($request['xls'] == '6') {
			$controller->display($request, 'emp/emp/list_drc');
		} else {
			$controller->display($request, 'emp/emp/list');
		}
		
		return true;
	}
	
	function card($controller, $request) {
		$this->pretreat($controller);
		
		$insql = $this->_batch_insql($request);
		if (empty($insql)) {
			$controller->assign('baseLink', $this->doGet($request, 'findPost'));

			$this->findPost($controller, $request);
			return false;
		}
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id %s";
		$sql = sprintf($fmt, $this->tbl, $insql);
		debug($sql);
		
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);

			$row = $this->_orderby_x($controller, $row, $this->orderby_s);

			$data[] = $row;
		}
		$controller->assign_by_ref('result', $data);

		$controller->display($request, 'emp/emp/card');
		return true;
	}
	
	function txt($controller, $request) {
		$this->pretreat($controller);

		$request['tpl'] = "txt";
		
		$insql = $this->_batch_insql($request);
		if (empty($insql)) {
			$controller->assign('baseLink', $this->doGet($request, 'findPost'));

			$this->findPost($controller, $request);
			return false;
		}
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id %s";
		$sql = sprintf($fmt, $this->tbl, $insql);
		debug($sql);
		
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_fn($row['name'], $row);
			
			$row['dept_t'] = $this->_dept($row['dept']);
			
			$data[] = $row;
		}
		$controller->assign_by_ref('result', $data);
		
		$controller->display($request, 'emp/emp/list');
		
		return true;
	}
	
	function ldif($controller, $request) {
		$this->pretreat($controller);

		$request['tpl'] = "txt";
		
		$insql = $this->_batch_insql($request);
		if (empty($insql)) {
			$controller->assign('baseLink', $this->doGet($request, 'findPost'));

			$this->findPost($controller, $request);
			return false;
		}
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id %s";
		$sql = sprintf($fmt, $this->tbl, $insql);
		debug($sql);
		
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_fn($row['name'], $row);
			
			$row['dept_t'] = $this->_dept($row['dept']);
			
			$data[] = $row;
		}
		$controller->assign_by_ref('result', $data);

		$controller->display($request, 'emp/emp/ldif');
		return true;
	}
	
	function _dept_main($dept) {
		$a = substr($dept, 0, 2);
		
		return Name::dept($a);
	}
	
	function _utf2gbk($s) {
		if (empty($s)) {
			return '';
		}
		
		return iconv("UTF-8", "GBK", $s);
	}
	
	function _fn($s, $ret) {
		if (empty($s)) {
			return '';
		}
		
		$ret['first'] = substr($s, 0, 3);
		$ret['last'] = substr($s, 3);
		
		return $ret;
	}
	
	function _batch_insql($request) {		
		$insql = "in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 4) == "c_1_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
		
		return $insql;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == 'exten') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['exten'];
		}
		
		if ($px == 'email') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['email'];
		}
		
		if ($px == 'im') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['im'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	/**
	 * 获得角色信息
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function roleGet($controller, $request) {
		$emp = $request['emp'];

		$db = newdb($this->dsn_r);
		$sql = "SELECT count(*) as t FROM " . Constant::tbl_emp_role . " where emp = '" . $emp . "'";
		// debug($sql);
		$result = $db->query($sql);
		$b = $db->fetch_array($result);

		$sql = "SELECT count(*) as t FROM " . Constant::tbl_emp_role . " where status = 1 and emp = '" . $emp . "'";
		// debug($sql);
		$result = $db->query($sql);
		$a = $db->fetch_array($result);

		return $a['t'] . '/' . $b['t'];
	}

	/**
	 * 获得权限信息
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function privGet($controller, $request) {
		$emp = $request['emp'];

		$db = newdb($this->dsn_r);
		$sql = "SELECT count(*) as t FROM " . Constant::tbl_emp_priv . " where emp = '" . $emp . "'";
		$result = $db->query($sql);
		$b = $db->fetch_array($result);

		$sql = "SELECT count(*) as t FROM " . Constant::tbl_emp_priv . " where status = 1 and emp = '" . $emp . "'";
		$result = $db->query($sql);
		$a = $db->fetch_array($result);

		return $a['t'] . '/' . $b['t'];
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);
		
		$controller->assign('indate', strtotime("20 year"));
		$controller->assign('birthday', strtotime("-20 year"));

		$controller->display($request, 'emp/emp/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);
		
		$emp = $_SESSION['id'];		
		$office = $this->_office($emp);
		
		$source = addslashes($request['source']);
		$date = $request['date'];

		$code = $request['code'];
		$name = $request['name'];

		$zjm = getPinyin($name);
		$zjm = strtoupper($zjm);
		$this->_zjm_clean($zjm);

		$idcard = $request['idcard'];
		$indate = $request['indate'];
		$fzjg = addslashes($request['fzjg']);
		
		$sex = $request['sex'];
		$birthday = $request['birthday'];
		
		$address = addslashes($request['address']);
		$province = $request['province'];
		$city = $request['city'];
		$district = $request['district'];

		$password = rand(1000, 9999);

		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %S WHERE code = '%'";
		$sql = sprintf($fmt, $this->tbl, $code);
		// debug($sql);
		$result = $db->query($sql);
		if ($db->num_rows($result) > 0) {
			$controller->assign('msg', "添加失败，编号重复！请重新添加。");
			$controller->display($request, 'info');
			return true;
		}

		$fmt = "INSERT INTO %s(office, name, zjm, password, idcard, indate, fzjg, sex, birthday, address, province, city, district, emp, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $name, $zjm, $password, $idcard, $indate, $fzjg, $sex, $birthday, $address, $province, $city, $district, $emp, $emp);
		// debug($sql);
		$db->query($sql);
		$id = $db->insert_id();
		
		require_once(dirname(__FILE__) . '/EmpJob.class.php');
		$aa = new EmpJob();
		$aa->_join($id, $source, $date, $emp);
		
		require_once(dirname(__FILE__) . '/EmpRole.class.php');
		$aa = new EmpRole();
		$aa->_update($id, '11');
		
		require_once 'Password.class.php';
		$passwd = new Password();
		$passwd->_aa($id);
		
		require_once 'Ldap.class.php';
		$ldap = new Ldap();
		// $ldap->_add($id, $password, $request);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function rejob($controller, $request) {
		$this->pretreat($controller);

		$idcard = $request['idcard'];
		
		$emp = $_SESSION['id'];

		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE idcard = '%s' ORDER BY id DESC";
		$sql = sprintf($fmt, $this->tbl, $idcard);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			$id = $row['id'];
		} else {
			$controller->assign('baseLink', $this->doGet($request, 'findPost'));

			$this->findPost($controller, $request);
			return true;
		}
		
		$fmt = "UPDATE %s SET state = 4, emp = '%s', status = 1, modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $emp, $id);
		debug($sql);
		$db->query($sql);
		
		$source = addslashes($request['source']);
		$date = $request['date'];
		
		require_once(dirname(__FILE__) . '/EmpJob.class.php');
		$aa = new EmpJob();
		$aa->_join($id, $source, $date, $emp);
		
		require_once(dirname(__FILE__) . '/EmpBank.class.php');
		$aa = new EmpBank();
		$aa->_update($id);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function ajax_exist_idcard($controller, $request) {
		$idcard = trim($request['idcard']);
		if (!$this->_idcard($idcard)) {
			debug("no idcard");
			echo "false";
			return false;
		}

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE idcard = '%s'";
		$sql = sprintf($fmt, $this->tbl, $idcard);
		debug($sql);
		$result = $db->query($sql);
		if ($db->num_rows($result) > 0) {
			echo "false";
		} else {
			echo "true";
		}

		return true;
	}
	
	function _idcard($s) {
		if (empty($s)) {
			return false;
		}
		
		if (strlen($s) == 15 || strlen($s) == 18) {
			return true;
		}
		
		if (preg_match("/[\d,|a-z|A-Z|\-|(|)]{6,12}$/", $s)) {
			return true;
		}
		
		return false;
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$row['out'] = $this->_out($row['exten']);
		
		$row['name_py'] = name_account(utf82gb($row['name']));
		$row['name_pinyin'] = str_py(utf82gb($row['name']));

		$controller->assign_by_ref('result', $row);
		
		$controller->assign_by_ref('sequence_option', range(0, 30, 1));

		if ($this->_my_edit()) {
			$controller->display($request, 'emp/emp/admin/edit');
		} else {
			$controller->display($request, 'emp/emp/edit');
		}

		return true;
	}
	
	function _my_edit() {
		if ($this->_is_admin()) {
			return true;
		}
		
		if ($this->_audit('26')) {
			return true;
		}
		
		return false;
	}
	
	function ajax_edit($controller, $request) {
		
		$id = $_COOKIE['id_'];
		
		$ret = array();
		if ($this->_ajax_x($id, 'emp')) {
			$ret['status'] = 1;
		} else if ($this->_ajax_x($id, 'oper')) {
			$ret['status'] = 1;
		} else if ($this->_audit('26')) {
			$ret['status'] = 1;
		} else {
			$ret['status'] = 2;
		}
		
		echo json_encode($ret);
	}

	function _out($exten) {
		require_once 'Exten.class.php';
		$aa = new Exten();
		
		return $aa->_out($exten);
	}

	function _exten_exist($id) {
		require_once 'Exten.class.php';
		$aa = new Exten();
		
		return $aa->_exist($id);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		$code = $request['code'];
		$name = $request['name'];

		$zjm = $request['zjm'];
		if (empty($zjm)) {
			$zjm = getPinyin($name);
			$zjm = strtoupper($zjm);
			$this->_zjm_clean($zjm);
		}

		$engname = $request['engname'];

		$office = $request['office'];
		$dept = $this->_input($request['dept']);
		
		$job = $request['job'];
		$orderby = $request['orderby'];

		$mobile = $request['mobile'];
		$short = $request['short'];

		$exten = $request['exten'];

		$email = strtolower($request['email']);
		$im = strtolower($request['im']);
		
		$book = trim($request['book'], "-");
		$state = trim($request['state'], "-");

		$content = addslashes($request['content']);
		$emp = $this->_input($request['emp']);

		$status = $request['status'];

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s set name = '%s', zjm = '%s', engname = '%s', office = '%s', dept = '%s', job = '%s', orderby = '%s', mobile = '%s', short = '%s'";
		$fmt .= ", exten = '%s', email = '%s', im = '%s', book = '%s', state = '%s', content = '%s', emp = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $name, $zjm, $engname, $office, $dept, $job, $orderby, $mobile, $short, $exten, $email, $im, $book, $state, $content, $emp, $status, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		} else {
			
			$row = $this->_view($id);
			if ($row['office'] <> $office || $row['dept'] <> $dept) {
				$aa = array();
				$aa['id'] = $id;
				$aa['office'] = $office;
				$aa['dept'] = $dept;
				$aa['status'] = $status;
				
				$this->_update_extra($aa);
			}
			
			clean($this->tbl . "_id_" . $id);
		}
		
		if ($request['im_openfire'] == 'openfire') {
			// $username = $this->_username($email, $name);
			$username = $request['im'];
			$password = rand(1000, 9999);
			// $password = substr($row['idcard'], -4);
			
			$this->_im($username, $password, $name, $email);
			
			$this->_im_vcard2($controller, $request);
		}
		
		require_once 'Ldap.class.php';
		$ldap = new Ldap();
		$ldap->_modify($id, $request);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _im($username, $password, $name, $email) {
		// $creationDate = time();
		$tbl = 'openfire.ofUser';
	
		$db = newdb("openfire");
		$fmt = "SELECT * FROM %s WHERE username = '%s'";
		$sql = sprintf($fmt, $tbl, $username);
		debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			return false;
		} else {
			$fmt = "INSERT INTO %s(username, plainPassword, name, email, creationDate, modificationDate) VALUES('%s', '%s', '%s', '%s', unix_timestamp(), unix_timestamp())";
			$sql = sprintf($fmt, $tbl, $username, $password, $name, $email);
		}
		
		debug($sql);
		$db->query($sql);
	}
	
	function _im_vcard2($controller, $request) {
		$id = $request['id'];
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $id);
		debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			$name = $row['name'];
			$email = $row['email'];
			
			$row = $this->_view(null, $row);
			
			$row = $this->_office_x($row);
			
			$username = $this->_username($email, $name);
			
			$photo = $this->_photo_base64($row['photo_t']);
			
			$row['photo_base64'] = $photo['base64'];
			$row['photo_type'] = $photo['type'];
			
			$controller->assign_by_ref('result', $row);
			
			$vcard = $this->_tj_html($controller, $request, 'emp/emp/vcard2');
			// echo $vcard;
				
			$this->_im_vcard($username, $vcard);
		}
	}
	
	function _update_extra($row) {
		require_once(dirname(__FILE__) . '/EmpJob.class.php');
		$job = new EmpJob();
		$job->_update_emp($row);
		
		require_once(dirname(__FILE__) . '/Education.class.php');
		$edu = new Education();
		$edu->_update_emp($row);
		
		require_once(dirname(__FILE__) . '/Contract.class.php');
		$contract = new Contract();
		$contract->_update_emp($row);
		
		require_once(dirname(__FILE__) . '/Family.class.php');
		$family = new Family();
		$family->_update_emp($row);
		
		require_once(dirname(__FILE__) . '/Work.class.php');
		$work = new Work();
		$work->_update_emp($row);
	}
	
	function _exten($id, $exten, $out) {
		
		require_once 'Exten.class.php';
		$aa = new Exten();
		
		return $aa->_exten_out($id);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function delete($controller, $request) {
		$this->pretreat($controller);

		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}
			
			$this->_delete3($id);
		}		

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _delete3($id) {
		$db = newdb($this->dsn_w);
		if ($this->_is_admin()) {
			$fmt = "DELETE FROM %s WHERE id = '%s'";
		} else {
			$fmt = "UPDATE %s SET visible = 2 WHERE id = '%s'";
		}
		
		$sql = sprintf($fmt, $this->tbl, $id);
		debug($sql);
		$db->query($sql);
	}
	
	function _delete3_priv() {
		$fmt = "DELETE FROM %s WHERE emp = '%s'";
		$sql = sprintf($fmt, Constant::tbl_emp_role, $id);
		// debug($sql);
		// $query = $db->query($sql);

		$fmt = "DELETE FROM %s WHERE emp = '%s'";
		$sql = sprintf($fmt, Constant::tbl_emp_priv, $id);
		// debug($sql);
		// $query = $db->query($sql);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);
		
		if (!empty($request['qr'])) {
			return $this->qr($controller, $request);
		}

		$id = $request['id'];
		$row = $this->_view($id);
		
		$row['photo_t'] = $this->_photo($row);
		
		$row['vacation'] = $this->_has_vacation($id);
		
		$controller->assign_by_ref('result', $row);

		// $controller->assign_by_ref('my', $_SESSION['emp']);
		if (!empty($request['view_qr'])) {
			$controller->display($request, 'emp/emp/view_qr');
			return true;
		}

		$controller->display($request, 'emp/emp/view');
		return true;
	}
	
	function _list() {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE status IN ('1', '2') ORDER BY id DESC";
		$sql = sprintf($fmt, $this->tbl);
		debug($sql);
		$result = $db->query($sql);
	
		return $db->fetch_all($result);
	}
	
	function _has_vacation($emp) {
		require_once(dirname(__FILE__) . '/../kq/Vacation.class.php');
		$vacation = new Vacation();
	
		return $vacation->_has_vacation($emp);
	}
	
	function qr($controller, $request) {
		require_once(APP_LIB . 'phpqrcode/qrlib.php');
		
		$id = $request['id'];
		$row = $this->_view($id);
		
		$row['url'] = "http://www.tecmold.com";
		$row['org'] = "景荣精密产品（深圳）有限公司";		
		
		$office = $row['office'];
		if ($office == '1') {
			$row["street"] = '广东省深圳市南山区南山大道1088号南园枫叶大厦8C';
			$tel = '0755-89809672';
			$row["fax"] = '0755-26416097';
		}
		
		if ($office == '2') {
			$row["street"] = '广东省深圳市宝安区沙井镇步涌同富裕工业区A-5地块B3-B4栋';
			$tel = '0755-88840348';
			// $row["fax"] = '0755-81444778';
			$row["fax"] = '0755-26416097';
		}
		
		if (!empty($row['exten'])) {
	    	$row["tel"] = $tel . '-'. $row['exten'];
	    }
	    
		$controller->assign_by_ref('result', $row);
		
		$t = $this->_tj_html($controller, $request, 'emp/emp/vcard');
		$value = $t;
		
		$errorCorrectionLevel = 'M';
		$matrixPointSize = 3;
		
		return QRcode::png($value, false, $errorCorrectionLevel, $matrixPointSize);
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['sex_t'] = $this->sex[$row['sex']];
		$row['state_t'] = $this->state[$row['state']];
		
		$row['j'] = $this->_job($row['id']);
		$row['c'] = $this->_contract($row['id']);

		$row['age'] = $this->age($row['birthday']);
		
		$row['mobile_t'] = $this->_mobile_t($row['mobile']);

		$row = $this->_view_t($row);
		
		return $row;
	}
	
	function _photo3($data) {
		$ret = Download::url2($this->info_dir, $data['id'], $data['photo']);
	
		if (empty($ret)) {
			return '/images/photo_none.jpg';
		}
	
		return $ret;
	}
	
	function _photo($data) {
		
		if (!class_exists("MogileFs")) {
			return false;
		}
		
		$key = sprintf("emp_%s", $data['id']);
		
		$mg = newfs('ydt');
		
		try {
			$paths = $mg->get($key);
			$ret = $paths['path1'];
			$mg->close();
		} catch(MogileFsException $e) {
			$ret = '/images/photo_none.jpg';
		}
		
		return $ret;
	}
	
	function _has_photo($emp) {
	
		if (!class_exists("MogileFs")) {
			return false;
		}
	
		$key = sprintf("emp_%s", $emp);
	
		$mg = newfs('ydt');
		try {
			$paths = $mg->get($key);
			$ret = $paths['path1'];
			$mg->close();
		} catch(MogileFsException $e) {
			return false;
		}
	
		return true;
	}
	
	function _mobile_t($s) {
		require_once(dirname(__FILE__) . '/../tel/Mobile.class.php');
		$aa = new Mobile();
		
		if ($row = $aa->_mobile($s)) {
			return $row['city'];
		}
		
		return '';
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function leave($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
		
		$controller->assign_by_ref('emp', $row);
		
		$controller->assign_by_ref('job', $this->_my_additional(Constant::tbl_emp_job, $id));

		$controller->display($request, 'emp/emp/leave');
		return true;
	}
	
	function _my_additional($tbl, $id) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE emp = '%s' ORDER BY id DESC";
		$sql = sprintf($fmt, $tbl, $id);
		// debug($sql);
		$result = $db->query($sql);

		return $db->fetch_all($result);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function leavePost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$db = newdb($this->dsn_w);
		if ($row['state'] == '6') {
			$fmt = "UPDATE %s SET `short` = '', status = '3', modifytime = now() WHERE id = '%s'";
		} else {
			$fmt = "UPDATE %s SET `short` = '', state = '8', status = '3', modifytime = now() WHERE id = '%s'";
		}
		$sql = sprintf($fmt, $this->tbl, $id);
		debug($sql);
		$db->query($sql);

		clean($this->tbl . "_id_" . $id);
		$row = $this->_view($id);
		
		require_once 'Ldap.class.php';
		$ldap = new Ldap();
		$ldap->_delete($row);
		
		$content = $request['content'];
		$t2 = $request['t2'];
		
		require_once 'EmpJob.class.php';
		$aa = new EmpJob();
		$aa->_leave($id, $t2, $content);
		
		require_once 'Contract.class.php';
		$bb = new Contract();
		$bb->_leave($id, $t2);
		
		require_once '/../wifi/User.class.php';
		$cc = new User();
		$cc->_leave($id);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));
		$this->findPost($controller, $request);
		return true;
	}

	function signature($controller, $request) {
		$this->pretreat($controller);

		$id = $_SESSION['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'emp/emp/signature');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function group($controller, $request) {
		$this->pretreat($controller);

		// $controller->assign('t1', strtotime('-100 day'));
		// $controller->assign('t2', strtotime('+1 day'));

		$controller->display($request, 'emp/emp/group');
		return true;
	}

	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'dept');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}

	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'dept') {
			$row['a'] = Name::dept($row['a']);
		} elseif ($px == 'sex') {
			$row['a'] = $this->sex[$row['a']];
		} elseif ($px == 'nationality') {
			$row['a'] = $this->nationality[$row['a']];
		} elseif ($px == 'education') {
			$row['a'] = $this->education[$row['a']];
		} elseif ($px == 'marriage') {
			$row['a'] = $this->marriage[$row['a']];
		} else {

			$row = $this->_name_t($row, $px);
		}
		
		return $row;
	}
	
	function audit_oper($controller, $request) {
		$id = $_COOKIE['id_'];
		$ret = array();
		
		if ($this->_is_admin()) {
			$ret['status'] = 1;
		} else if ($this->_audit('7800')) {
			$ret['status'] = 1;
		} else if ($this->_ajax_x($id, 'oper')) {
			$ret['status'] = 1;
		} else {
			$ret['status'] = 2;
		}

		echo json_encode($ret);
	}

	/**
	 * 接口
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function dept($controller, $request) {
		return $this->ajax_dept($controller, $request);
	}

	/**
	 * 接口
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function ajax_dept($controller, $request) {
		if (!empty($request['dept'])) {
			$dept = $request['dept'];
		}
		if (!empty($request['dept_s'])) {
			$dept = $request['dept_s'];
		}

		if (empty($dept) || $dept == '-') {
			$data[] = array('id'=>'-', 'name'=>'请选择');
			echo json_encode($data);
			return false;
		}

		$key = "emp.dept_" . $dept;
		// debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				echo json_encode($ret);
				return true;
			}
		}

		$db = newdb($this->dsn_r);
		$fmt = "SELECT id, name, dept, job, mobile, exten FROM %s WHERE status = '1' AND dept = '%s' ORDER BY orderby";
		$sql = sprintf($fmt, Constant::tbl_emp, $dept);
		// debug($sql);
		$result = $db->query($sql);
		$ret[] = array('id'=>'-', 'name'=>'请选择');
		$ret = array_merge($ret, $db->fetch_all($result));
		if (is_object($mc) && count($ret) > 0) {
			$mc->set($key, $ret, 0, _strtotime("1 day"));
		}

		echo json_encode($ret);
		return true;
	}
	
	function ajax_emp($controller, $request) {
		
		$id = $request['id'];
		
		$ret = array('status'=>'0');
		
		if ($row = $this->_view($id)) {
			$ret['id'] = $row['id'];
			$ret['office'] = $row['office'];
			$ret['dept'] = $row['dept'];
			$ret['name'] = $row['name'];
			$ret['idcard'] = $row['idcard'];
			
			$ret['status'] = $row['status'];
			
			// print_r($ret);
		}
		
		echo json_encode($ret, JSON_UNESCAPED_UNICODE);
	}
	
	function ajax_test($controller, $request) {
		$url = "http://localhost/t/admin/emp.php?do=Emp.ajax_emp&id=101";
		// echo $url;
		
		$ret = @file_get_contents($url);
		// echo $ret;
		
		$data = json_decode($ret, true);
		
		print_r($data);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function ajax_zjm($controller, $request) {
		$data = $this->_ajax_zjm($request);
		echo json_encode($data);
	}

	function _ajax_zjm($request) {
		$q = $request['q'];
		$n = $request['maxRows'];
		if (empty($n)) {
			$n = 20;
		}
		
		if (preg_match("/^(\d+)(.+)/", $q, $m)) {
			$status = $m[1];
			$zjm = $m[2];
		} else {
			$status = 1;
			$zjm = $q;
		}

		if (empty($zjm)) {
			debug("OK" . $zjm);
			return array();
		}

		if ($status == 1) {
			$key = sprintf("%s_%s_%s", $this->zjm, $this->prefix, $zjm);
		} else {
			$key = sprintf("%s_%s_%s_%s", $this->zjm, $this->prefix, $status, $zjm);
		}
		
		debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}

		$db = newdb($this->dsn_r);
		$fmt = "SELECT id, code, name, zjm, dept, exten, `short`, status FROM %s WHERE visible = 1 AND status = '%s' AND zjm LIKE '%s' ORDER BY zjm LIMIT %s";
		$sql = sprintf($fmt, $this->tbl, $status, $zjm . "%", $n);
		debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);

			$ret[] = $row;
		}
		
		if (is_object($mc) && count($ret) > 0) {
			$mc->set($key, $ret, 0, _strtotime("1 day"));
		}

		return $ret;
	}

	function _zjm($s) {

		if (preg_match("/^1/", $s)) {
			return substr($s, 1);
		}

		return $s;
	}

	function z($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s";
		$sql = sprintf($fmt, $this->tbl);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$zjm = getPinyin($row['name']);
			$zjm = strtoupper($zjm);

			$dept = $this->_input($row['dept']);

			$fmt = "UPDATE %s SET dept = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $dept, $row['id']);
			// debug($sql);
			$db->query($sql);
		}

		logdate($controller, $request);
		return true;
	}

	function _audit($service) {
		if ($this->_is_admin()) {
			return true;
		}

		return parent::_audit($service);
	}
	
	function tj1($controller, $request) {
		$a = array(
			'南山员工'=>array('a'=>'326', 'office'=>'1'),
			'沙井员工'=>array('a'=>'327', 'office'=>'2'),
		);
		
		while (list($k, $v) = each($a)) {
			$request['title'] = $k;
			$request['a'] = $v['a'];
			$request['office'] = $v['office'];
						
			$this->tj($controller, $request);
		}
	}
	
	function tj2($controller, $request) {
		$a = array(
			'南山员工（周报）'=>array('a'=>'326', 'office'=>'1'),
			'沙井员工（周报）'=>array('a'=>'327', 'office'=>'2'),
		);
		
		while (list($k, $v) = each($a)) {
			$request['title'] = $k;
			$request['a'] = $v['a'];
			$request['office'] = $v['office'];
			
			$this->tjw($controller, $request);
		}				
	}

	function tj($controller, $request) {
		$bb = $this->_list_email($request['a']);
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}
		
		$from = $this->email;
		$subject = $request['title'];		
		$office = $request['office'];
		
		$cc = array(
			'离职信息'=>array('w' => "office = '$office' AND DATE_FORMAT(modifytime, '%Y-%m-%d') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d') AND status = 3", 'b'=>'status'),	
			'新增信息'=>array('w' => "office = '$office' AND DATE_FORMAT(createtime, '%Y-%m-%d') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d')", 'b'=>'status'),
			'更新信息'=>array('w' => "office = '$office' AND DATE_FORMAT(modifytime, '%Y-%m-%d') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d') AND DATE_FORMAT(createtime, '%Y-%m-%d') <> DATE_FORMAT(modifytime, '%Y-%m-%d') AND status = '1'", 'b'=>'status'),			
		);
		
		if ($this->_tj_cc($controller, $cc)) {
			echo "222";
			return;
		}
		
		$controller->clear_assign('result');

		$body = $this->_tj_html($controller, $request, 'emp/emp/tj_list');
		
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}
	
	function tjw($controller, $request) {
		$bb = $this->_list_email($request['a']);
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}
		
		$from = $this->email;
		$subject = $request['title'];		
		$office = $request['office'];
		
		$cc = array(
			'离职信息'=>array('w' => "office = '$office' AND DATE_FORMAT(modifytime, '%Y-%V') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 WEEK), '%Y-%V') AND status = 3", 'b'=>'status'),	
			'新增信息'=>array('w' => "office = '$office' AND DATE_FORMAT(createtime, '%Y-%V') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 WEEK), '%Y-%V')", 'b'=>'status'),
			'更新信息'=>array('w' => "office = '$office' AND DATE_FORMAT(modifytime, '%Y-%V') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 WEEK), '%Y-%V') AND DATE_FORMAT(createtime, '%Y-%m-%d') <> DATE_FORMAT(modifytime, '%Y-%m-%d') AND status ='1'", 'b'=>'status'),			
		);
		
		$this->_tj_cc($controller, $cc);
		
		$aa = array(
			'一个星期之内'=>array('w' => "WHERE office = '$office' AND createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -7 DAY), '%Y-%m-%d')"),
			'一个月之内'=>array('w' => "WHERE office = '$office' AND createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 MONTH), '%Y-%m-%d')"),
			'三个月之内'=>array('w' => "WHERE office = '$office' AND createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -3 MONTH), '%Y-%m-%d')"),
			'半年之内'=>array('w' => "WHERE office = '$office' AND createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -6 MONTH), '%Y-%m-%d')"),
			'一年之内'=>array('w' => "WHERE office = '$office' AND createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 YEAR), '%Y-%m-%d')"),
			'全部'=>array('w' => "")
		);
		
		$this->_tj_bb($controller, $aa);

		$body = $this->_tj_html($controller, $request, 'emp/emp/tj_list');
		
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}
	
	function _tj2($v) {
		$this->result_s = 10;
		$this->groupby_s = 'dept';
			
		$s = $this->groupby_s;

		if (!empty($v['w'])) {
			$fmt = "SELECT %s AS a, COUNT(id) AS c FROM %s a  %s GROUP BY %s ORDER BY c DESC";
			$sql = sprintf($fmt, $s, $this->tbl, $v['w'], $s);
		} else {
			$fmt = "SELECT %s AS a, COUNT(id) AS c FROM %s a GROUP BY %s ORDER BY c DESC";
			$sql = sprintf($fmt, $s, $this->tbl, $s);
		}		
		echo $sql . "\n";

		return $this->_group_tj2($sql);
	}

	function f($controller, $request) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE status = 1";
		$sql = sprintf($fmt, $this->tbl);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$fmt = "UPDATE %s SET dept = '%s' WHERE id = '%s';\n";
			$sql = sprintf($fmt, $this->tbl, $row['dept'], $row['id']);
			echo $sql;
		}

		echo "\n" . $_REQUEST['do'] . "\n";

		logdate($controller, $request);
		return true;
	}
	
	function ajax_print1($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'print');
		return true;
	}
	
	function ajax_print2($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'emp/emp/print');
		return true;
	}
	
	function vcf($controller, $request) {
		header('Content-Disposition:text/vcf; filename=user.vcf');
		header('Content-Type:text/vcf;charset=utf8');

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE status = 1";
		$sql = sprintf($fmt, $this->tbl);
		debug($sql);
		$result = $db->query($sql);

		$controller->assign_by_ref('result', $db->fetch_all($result));

		$controller->display($request, 'emp/emp/vcf');
		return true;
	}
	
	function dh($controller, $request) {
		$db = newdb($this->dsn_w);

		$file = "/home/sunxy/b.csv";
		$fd = fopen($file, "r");
		while (!feof($fd)) {
			$buf = trim(fgets($fd));
			// echo $buf;

			if (strlen($buf) < 5 || preg_match("/^#/i", $buf)) {
				continue;
			}
			
			$a = preg_split("/,/", $buf);

			// print_r($a);

			$mobile = trim($a[1]);
			$short = trim($a[2]);
			
			$fmt = "UPDATE %s SET short = '%s' WHERE mobile = '%s'";
			$sql = sprintf($fmt, $this->tbl, $short, $mobile);
			echo $sql . "\n";
			$db->query($sql);
		}
		fclose($fd);

		echo "\n" . $_REQUEST['do'] . "\n";

		logdate($controller, $request);
		return true;
	}
	
	function dh_del($controller, $request) {
		$db = newdb($this->dsn_w);

		$file = "/home/sunxy/b.csv";
		$fd = fopen($file, "r");
		while (!feof($fd)) {
			$buf = trim(fgets($fd));
			// echo $buf;

			if (strlen($buf) < 5 || preg_match("/^#/i", $buf)) {
				continue;
			}
			
			$a = preg_split("/,/", $buf);

			// print_r($a);

			$mobile = trim($a[1]);
			$short = trim($a[2]);
			if ($this->_mobile($mobile) == '3') {
				echo $buf . "\n";
			}
		}
		fclose($fd);

		echo "\n" . $_REQUEST['do'] . "\n";

		logdate($controller, $request);
		return true;
	}
	
	function _mobile($mobile) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE mobile = '%s'";
		$sql = sprintf($fmt, $this->tbl, $mobile);
		$result = $db->query($sql);		
		if ($row = $db->fetch_array($result)) {
			return $row['status'];
		}
		
		return '1';
	}
	
	function u($controller, $request) {
		require_once(dirname(__FILE__) . '/Password.class.php');
		$password = new Password();
		
		require_once(dirname(__FILE__) . '/Role.class.php');
		$role = new Role();		
		
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE status = '1'";
		// $fmt = "SELECT * FROM %s WHERE status = '1' LIMIT 10";
		$sql = sprintf($fmt, $this->tbl);
		// echo $sql . "\n";
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$password->_aa($row['id']);
			$role->_update($row['id'], '11');
		}
		
		logdate($controller, $request);
		return true;
	}
	
	function sync_status($controller, $request) {
		require_once(dirname(__FILE__) . '/Family.class.php');
		$ff = new Password();
		
		require_once(dirname(__FILE__) . '/Education.class.php');
		$ee = new Password();
		
		require_once(dirname(__FILE__) . '/Work.class.php');
		$ww = new Password();
	
		$dd = date("Y-m-d", strtotime('-3 day'));
		
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE modifytime > '%s'";
		$sql = sprintf($fmt, $this->tbl, $dd);
		echo $sql . "\n";
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$ff->_update_emp($row['status']);
			$ee->_update_emp($row['status']);
			$ww->_update_emp($row['status']);
		}
	
		logdate($controller, $request);
		return true;
	}
	
	function ldap($controller, $request) {
		
		require_once(dirname(__FILE__) . '/Ldap.class.php');
		$ldap = new Ldap();
		
		$ds = $ldap->_connect();
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s limit 5";
		$fmt = "SELECT * FROM %s WHERE book = '1' AND status = '1'";
		$fmt = "SELECT * FROM %s WHERE status = '1'";
		
		// $fmt = "SELECT * FROM %s WHERE modifytime > DATE_ADD(now(), INTERVAL -7 DAY)";
		$sql = sprintf($fmt, $this->tbl);
		echo $sql . "\n";
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			if (empty($row['dept'])) {
				continue;
			}
			
			$ldap->_abcd($ds, $row);
		}
		
		ldap_close($ds);

		logdate($controller, $request);
		return true;
	}
	
	function _emp_email($email) {
		if ($row = Common::x($this->tbl, "email", $email)) {
			$row = $this->_view(null, $row);
		
			return $row;
		}
		
		return false;
	}
	
	function _emp_im($s) {
		if ($row = Common::x($this->tbl, "im", $s)) {
			$row = $this->_view(null, $row);
	
			return $row;
		}
	
		return false;
	}
	
	function tj3($controller, $request) {
		$a = array(
			'未交银行工资卡[南山员工]'=>array('a'=>'347', 'office'=>'1'),
			'未交银行工资卡[沙井员工]'=>array('a'=>'348', 'office'=>'2'),
			'未交银行工资卡[清溪员工]'=>array('a'=>'350', 'office'=>'3'),
		);
		
		while (list($k, $v) = each($a)) {
			$request['title'] = $k;
			$request['a'] = $v['a'];
			$request['office'] = $v['office'];
						
			$this->bank($controller, $request);
		}
	}
	
	function bank($controller, $request) {
		// $bb = $this->_list_email('321');
		$bb = $this->_list_email($request['a']);
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}

		$from = $this->email;
		$subject = $request['title'];
		// $subject = '未交银行工资卡的员工';

		$body = $this->_bank($controller, $request);
		// echo $body;
		
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}
	
	function _bank($controller, $request) {
		require_once(dirname(__FILE__) . '/EmpBank.class.php');
		$aa = new EmpBank();
			
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE office = '%s' AND state IN ('4', '5', '6') AND status = '1' ORDER BY id";
		$sql = sprintf($fmt, $this->tbl, $request['office']);
		echo $sql . "\n";
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			if ($aa->_exist($row['id'])) {
				continue;
			}
			
			$row['sex_t'] = $this->sex[$row['sex']];
			$row['age'] = $this->age($row['birthday']);	
			$row['dept_t'] = $this->_dept($row['dept']);
			
			$row['j'] = $this->_job($row['id']);
		
			$data[] = $row;
		}
		
		$controller->assign_by_ref('result', $data);
		
		return $this->_tj_html($controller, $request, 'emp/emp/bank');
	}
	
	function tj4($controller, $request) {
		$a = array(
			'未添加相片[南山员工]'=>array('a'=>'345', 'office'=>'1'),
			'未添加相片[沙井员工]'=>array('a'=>'346', 'office'=>'2'),
			'未添加相片[清溪员工]'=>array('a'=>'351', 'office'=>'3'),
		);
		
		while (list($k, $v) = each($a)) {
			$request['title'] = $k;
			$request['a'] = $v['a'];
			$request['office'] = $v['office'];
						
			$this->photo($controller, $request);
		}
	}
	
	function photo($controller, $request) {
		// $bb = $this->_list_email('345');
		$bb = $this->_list_email($request['a']);
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}

		$from = $this->email;
		// $subject = '未添加相片的员工';
		$subject = $request['title'];

		$body = $this->_photo2($controller, $request);
		// echo $body;
		
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}
	
	function _photo2($controller, $request) {
			
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE office = '%s' AND state IN ('4', '5', '6') AND status = '1' ORDER BY id";
		$sql = sprintf($fmt, $this->tbl, $request['office']);
		echo $sql . "\n";
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			if ($this->_has_photo($row['id'])) {
				continue;
			}
						
			$row['sex_t'] = $this->sex[$row['sex']];
			$row['age'] = $this->age($row['birthday']);	
			$row['dept_t'] = $this->_dept($row['dept']);
			
			$row['j'] = $this->_job($row['id']);
		
			$data[] = $row;
		}
		
		$controller->assign_by_ref('result', $data);
		
		return $this->_tj_html($controller, $request, 'emp/emp/bank');
	}
	
	function _job($emp) {
		require_once(dirname(__FILE__) . '/EmpJob.class.php');
		$aa = new EmpJob();
		
		return $aa->_lastest($emp);
	}
	
	function _contract($emp) {
		require_once(dirname(__FILE__) . '/Contract.class.php');
		$aa = new Contract();
	
		return $aa->_lastest($emp);
	}
	
	function bank2($controller, $request) {
	
		$id = $request['id'];
		
		$controller->assign_by_ref('emp', $this->_view($id));
		
		require_once(dirname(__FILE__) . '/../finance/Bank.class.php');
		$bank = new Bank();
		
		$bank->emp($controller, $request);
	}
	
	function bankPost($controller, $request) {
		require_once(dirname(__FILE__) . '/../finance/Bank.class.php');
		$bank = new Bank();
		
		$bank->empPost($controller, $request);
		
		$this->bank2($controller, $request);
	}
	
	function ajax_bank($controller, $request) {
		$name = $request['name'];
		$code = $request['code'];
	
		require_once(dirname(__FILE__) . '/../finance/Bank.class.php');
		$bank = new Bank();
	
		$ret = array('status'=>'1');
		if ($bank->_name_code($name, $code)) {
			$ret['status'] = '2';
		}
	
		echo json_encode($ret);
	}
	
	function _by_username($s) {
		$db = newdb($this->dsn_r);
		
		if (preg_match("/^6\d{5}$/i", $s)) {
			$fmt = "SELECT * FROM %s WHERE status in ('1', '8') AND `short` = '%s'";
			$sql = sprintf($fmt, $this->tbl, $s);
		} else if (preg_match("/^1(3|4|5|8)\d{9}$/i", $s)) {
			
			$fmt = "SELECT * FROM %s WHERE status in ('1', '8') AND mobile = '%s'";
			$sql = sprintf($fmt, $this->tbl, $s);
		} else if (preg_match("/^\d+$/i", $s)) {
			$fmt = "SELECT * FROM %s WHERE status in ('1', '8') AND (id = '%s' OR exten = '%s')";
			$sql = sprintf($fmt, $this->tbl, $s, $s);
		} else {
			$fmt = "SELECT * FROM %s WHERE status in ('1', '8') AND name = '%s'";
			$sql = sprintf($fmt, $this->tbl, $s);
		}
		
		debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			return $row;
		}
		
		return false;
		
	}
	
	function t($controller, $request) {
		require_once(dirname(__FILE__) . '/Mac.class.php');
		$aa = new Mac();
	
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE status = '1' limit 1000";
		$sql = sprintf($fmt, $this->tbl);
		echo $sql . "\n";
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			// print_r($row);
			$name = $row['name'];
			$email = $row['email'];
			if (!$aa->_exist_emp($row['id']) && empty($email)) {
				continue;
			}
			
			$row = $this->_view(null, $row);
			
			$row = $this->_office_x($row);
			
			$username = $this->_username($email, $name);
			
			$photo = $this->_photo_base64($row['photo_t']);
			
			$row['photo_base64'] = $photo['base64'];
			$row['photo_type'] = $photo['type'];
			
			$controller->assign_by_ref('result', $row);
			
			$vcard = $this->_tj_html($controller, $request, 'emp/emp/vcard2');
			// echo $vcard;
	
			$this->_im_vcard($username, $vcard);
			
			$this->_x_im($db, $row['id'], $username);
		}
	
		logdate($controller, $request);
		return true;
	}
	
	function _x_im($db, $id, $username) {
		$fmt = "UPDATE %s SET im = '%s' WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $username, $id);
		$db->query($sql);
	}
	
	function _office_x($row) {
		$office = $row['office'];
		
		if ($office == '1') {
			$row["street"] = '广东省深圳市南山区南山大道1088号南园枫叶大厦8C';
			$row['tel'] = '0755-89809672';
			$row["fax"] = '0755-26416097';
			$row['pcode'] = '518054';
		}
		
		if ($office == '2') {
			$row["street"] = '广东省深圳市宝安区沙井镇步涌同富裕工业区A-5地块B3栋';
			$row['tel'] = '0755-88840348';
			// $row["fax"] = '0755-81444778';
			$row["fax"] = '0755-26416097';
			$row['pcode'] = '518104';
		}
		
		if ($office == '3') {
			$row["street"] = '广东省东莞市清溪镇三中村顺峰路景荣厂';
			$row['tel'] = '0769-39012955';
			// $row["fax"] = '0755-81444778';
			$row["fax"] = '0769-39012798';
			$row['pcode'] = '523651';
		}
		
		return $row;
	}
	
	function _photo_base64($url) {
		$ret = array();
		
		if (empty($url)) {
			return $ret;
		}
		
		global $cfg;
		
		$url = $cfg['upload'] . $url;
		// $url = sprintf("%s%s", 'http://ns.tecmold.net', $url);
		
		$size = getimagesize($url);
		$ret['type'] = $size['mime'];
		
		// $s = "http://ns.tecmold.com/upload/tt/emp/964/3082524854b984dad3c1b1.JPG";
		// echo $url . "\n";
		$buf = file_get_contents($url);
		// debug($buf);
	
		$ret['base64'] = chunk_split(base64_encode($buf));
		
		return $ret;
	}
	
	function _username($email, $name) {
		$email = trim($email);
		$name = trim($name);
	
		if (empty($email)) {
			return name_account(utf82gb($name));
		}
	
		list($name, $server) = explode('@', $email);
	
		return $name;
	}
	
	function _im_vcard($username, $vcard) {
		$tbl = 'openfire.ofVCard';
		
		$vcard = addslashes($vcard);
		
		$db = newdb("openfire");
		$fmt = "SELECT * FROM %s WHERE username = '%s'";
		$sql = sprintf($fmt, $tbl, $username);
		// echo $sql . "\n";
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			// $vcard = 'aa';
			$fmt = "UPDATE %s SET vcard = '%s' WHERE username = '%s'";
			$sql = sprintf($fmt, $tbl, $vcard, $username);
			
		} else {
			$fmt = "INSERT INTO %s(username, vcard) VALUES('%s', '%s')";
			$sql = sprintf($fmt, $tbl, $username, $vcard);
		}
		// echo $sql . "\n";
		$db->query($sql);
	}
	
	function role($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		$emp = $this->id($this->tbl, $id);
		
		$controller->assign_by_ref('emp', $emp);
	
		require_once(dirname(__FILE__) . '/../info/Role.class.php');
		$aa = new Role();
		
		$role = $aa->_list();
		
		require_once(dirname(__FILE__) . '/EmpRole.class.php');
		$bb = new EmpRole();
		
		$data = array();
		$data_all = array();
		$no = 1;
		while (list($k, $row) = each($role)) {
			$row['no'] = $no++;
				
			if ($bb->_emp_role($id, $row['id'])) {
				$row['class'] = "class='added'";
				$data[] = $row;
			}
	
			$data_all[] = $row;
		}
		$controller->assign_by_ref('result', $data);
		$controller->assign_by_ref('result_all', $data_all);
	
		$controller->display($request, 'emp/emp/role');
		return true;
	}
	
	function rolePost($controller, $request) {
		$emp = $request['id'];
		$list = $request['list'];
		
		require_once(dirname(__FILE__) . '/EmpRole.class.php');
		$aa = new EmpRole();
		
		$aa->_reset_emp($emp);
		
		if (empty($list)) {
			return false;
		}
	
		$bb = explode(',', $list);
		while (list($no, $role) = each($bb)) {
			$aa->_update($emp, $role);
		}
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function priv($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		$emp = $this->id($this->tbl, $id);
		
		$controller->assign_by_ref('emp', $emp);
		
		require_once(dirname(__FILE__) . '/../info/Priv.class.php');
		$aa = new Priv();
		
		$priv = $aa->_list();
		
		require_once(dirname(__FILE__) . '/EmpPriv.class.php');
		$bb = new EmpPriv();
		
		$data = array();
		$no = 1;
		while (list($k, $row) = each($priv)) {
			$row['no'] = $no++;
				
			if ($bb->_priv_emp($row, $id)) {
				$row['class'] = "class='added'";
				$data[] = $row;
			}
	
			$data_all[] = $row;
		}
		$controller->assign_by_ref('result', $data);
		$controller->assign_by_ref('result_all', $data_all);

		$controller->display($request, 'emp/emp/priv');
		return true;
	}
	
	/**
	 * 权限分配
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function privPost($controller, $request) {
		$emp = $request['id'];
		$list = $request['list'];
	
		require_once(dirname(__FILE__) . '/EmpPriv.class.php');
		$aa = new EmpPriv();
		
		$aa->_reset_emp($emp);
	
		if (empty($list)) {
			return false;
		}
	
		$bb = explode(',', $list);
		while (list($k, $priv_id) = each($bb)) {
			$aa->_update($emp, $priv_id);
		}
	
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function test($controller, $request) {

		$controller->display($request, 'emp/emp/photo');
		return true;
	}
	
	function ajax_photo($controller, $request) {
		
		$data = $request['img'];
		$image = base64_decode(str_replace("data:image/jpeg;base64,", "", $data));
		
		$fp = fopen("/Users/sunxiaoye/abc.jpg", 'w');
		fwrite($fp, $image);
		fclose($fp);
		
		$ret = array('status'=>'1');
		echo json_encode($ret);
	}
}
?>
